﻿using System.Linq;
using System.Net;
using Enyim.Caching;
using System.Web.Mvc;
using Web.Manage.Util;
using SilenthinkContract;
using Web.Manage.Models;
using SilenthinkContract.Domain;
using System.Collections.Generic;
using SilenthinkCommon.Extension;
namespace Web.Manage.Controllers
{
    public class ProductController : BaseController
    {
        public ProductController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient) : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index(int size = 20, int index = 1, string name = "")
        {
            var model = new ProductControllerModel.Index();
            model.Products = StoreService.GetPagedProductList(size, index, name).ToPagedList(index, size);
            return View(model);
        }

        public ActionResult Create(string productCategoryCode)
        {
            var random = GenerateRandom(5, RandomType.NumberAndUppercased);
            var productCode = productCategoryCode + "-" + random;
            var model = new ProductControllerModel.Create(productCode, productCategoryCode);

            var productAttributeNames = StoreService.GetProductAttributeName(productCategoryCode, string.Empty, null, true);
            var attributeNameCodeArray = productAttributeNames.Select(o => o.AttributeNameCode).ToArray();
            var productAttributeValues = StoreService.GetProductAttributeValue(productCategoryCode, true, attributeNameCodeArray);

            model.ProductAttributeNames = productAttributeNames.ToList();
            model.ProductAttributeValues = productAttributeValues.ToList();

            return View(model);
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult Create(Product model, string[] attributes, List<ProductControllerModel.ProductVariantSaleAttribute> saleAttributes)
        {
            model.ProductExtra.FullDescription = model.ProductExtra.FullDescription.GetSafeHtmlFragment();
            var random = GenerateRandom(5, RandomType.NumberAndUppercased);
            model.ProductCode = model.CategoryCode + "-" + random;
            model.CreatedBy = "";
            ProductHelper.ProcessAttributes(model, attributes, saleAttributes);
            var result = StoreService.CreateProduct(model);

            //var result =new StoreService().CreateProduct(model);
            return Json(result ? HttpStatusCode.OK : HttpStatusCode.BadRequest, result ? "添加成功" : "添加失败");
        }

        public ActionResult Detail(long param)
        {
            var model = new ProductControllerModel.Detail();
            model.Product = StoreService.GetProduct(param, true, true);
            return View(model);
        }

        public ActionResult Category()
        {
            return PartialView();
        }

    }
}
